#include "chat.h"
#include "ui_chat.h"

Chat::Chat(QTcpSocket *s, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Chat)
{
    ui->setupUi(this);
    socket = s;

    //客户端收到服务器发送的信息，socket发出readyread信号
    connect(socket,&QTcpSocket::readyRead,this,&Chat::serverInfoSlot);

}

Chat::~Chat()
{
    delete ui;
}

void Chat::on_clearButton_clicked()
{
    ui->lineEdit->clear();
}


void Chat::on_sendButton_clicked()
{
    //设置发送按钮的快捷键
    this->ui->sendButton->setShortcut(Qt::Key_Enter | Qt::Key_Return);
    QByteArray ba;
    ba.append(ui->lineEdit->text().toUtf8());
    socket->write(ba);
}

void Chat::serverInfoSlot()
{
    //获取信号的发送者
    QTcpSocket *s = (QTcpSocket *)sender();
    ui->mainLineEdit->setText(QString(s->readAll()));
}
